import '@tiptap/extension-text-style';
import { Extension } from '@tiptap/core';

const Indent = Extension.create({
    name: 'indent',
    addOptions() {
        return {
            types: ['paragraph'],
        };
    },
    addGlobalAttributes() {
        return [
            {
                types: this.options.types,
                attributes: {
                    Indent: {
                        default: 0,
                        parseHTML: element => {
                            var _a; return (_a = element.style['padding-left']) === null || _a === void 0 ? void 0 : _a.replace(/['"]+/g, '');
                        },
                        renderHTML: attributes => {
                            if (!attributes.Indent) {
                                return {};
                            }
                            return {
                                style: `padding-left: ${attributes.Indent}em`,
                            };
                        },
                    },
                },
            },
        ];
    },
    addCommands() {
        return {
             setUpIndent: () => (arg) => {
                 const { state, dispatch } = arg
                 const { selection } = state
                 const { from, to } = selection
                 const tr = state.tr
                 state.doc.nodesBetween(from, to, (node, pos) => {
                     if (node.isBlock) {
                         let currentIndent = node.attrs.Indent || 0
                         const newIndent = currentIndent >= 0 ? ++currentIndent : 0
                         tr.setNodeMarkup(pos, null, {
                             ...node.attrs,
                             Indent: newIndent
                         })
                     }
                 })

                 if (tr.docChanged) {
                     dispatch && dispatch(tr)
                     return true
                 }
                 return false
            },
            setDownIndent: () => (arg) => {
                const { state, dispatch } = arg
                const { selection } = state
                const { from, to } = selection
                const tr = state.tr
                state.doc.nodesBetween(from, to, (node, pos) => {
                    if (node.isBlock) {
                        let currentIndent = node.attrs.Indent || 0
                        const newIndent = currentIndent >= 0 ? --currentIndent : 0
                        tr.setNodeMarkup(pos, null, {
                            ...node.attrs,
                            Indent: newIndent
                        })
                    }
                })

                if (tr.docChanged) {
                    dispatch && dispatch(tr)
                    return true
                }
                return false
            },
            unsetIndent: () => (arg) => {
                const { state, dispatch } = arg
                const tr = state.tr
                const { selection } = state
                const { from, to } = selection
                state.doc.nodesBetween(from, to, (node, pos) => {
                    if (node.isBlock) {
                        tr.setNodeMarkup(pos, null, {
                            ...node.attrs,
                            Indent: 0
                        })
                    }
                })
                if (tr.docChanged) {
                    dispatch && dispatch(tr)
                    return true
                }

                return false
            },
        };
    },
});

export { Indent, Indent as default };